package com.testTimeTag;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class TagRecord {
	
	public static DateFormat dfYYYYMMDD=new SimpleDateFormat("yyyy-MM-dd");
	private List<Date> absent=new ArrayList<Date>();
	private List<Date> clockInLost=new ArrayList<Date>();
	private List<Date> clockOutLost=new ArrayList<Date>();
	private String dept=null;
	private String name=null;
	private List<Record12OneDay> record=new ArrayList<Record12OneDay>();
	private String recordnum=null;
	public TagRecord(){
		
	}
	public TagRecord(String dept,String name,String recordnum){
		this.dept=dept;
		this.name=name;
		this.recordnum=recordnum;
	}
	public void addAbsent(Date d){
		absent.add(d);
	}
	public void addClockInLost(Date d){
		clockInLost.add(d);
	}
	public void addClockOutLost(Date d){
		clockOutLost.add(d);
	}
	/**
	 * 
	 * @param date 
	 * @param month 要计算的月份
	 */
	public void addRecord(Date date,int month){
		Calendar calendar1=Calendar.getInstance();
		Calendar calendar2=Calendar.getInstance();
		calendar2.setTime(date);
		int day2=calendar2.get(Calendar.DAY_OF_MONTH);
		if(day2==1 && calendar2.get(Calendar.MONTH)==month){
			Calendar c=Calendar.getInstance();
			c.setTime(date);
			c.add(Calendar.MONTH, -1);
			day2=c.getActualMaximum(Calendar.DAY_OF_MONTH)+day2;
		}
		int hour2=calendar2.get(Calendar.HOUR_OF_DAY);
		//取每天12点前的最后一次打卡记录和12点后的第一次打卡记录
		if(record.size()>0){
			Record12OneDay record12OneDay=record.get(record.size()-1);
			Date date1=record12OneDay.getDate1();
			Date date2=record12OneDay.getDate2();
			//如果上一次最后打卡记录不为空时
			if(date2!=null){
				calendar1.setTime(date2);
				int day1=calendar1.get(Calendar.DAY_OF_MONTH);
				if(day1==1 && calendar1.get(Calendar.MONTH)==month){
					Calendar c=Calendar.getInstance();
					c.setTime(date);
					c.add(Calendar.MONTH, -1);
					day1=c.getActualMaximum(Calendar.DAY_OF_MONTH)+day1;
				}
				int hour1=calendar1.get(Calendar.HOUR_OF_DAY);
				boolean theSameDay=day1==day2;
				if(!theSameDay){
					//如果不是同一天，则当hour2位于12点前时，记录最后一次12点前的记录，当hour2位于12点后时，记录第一次12点后的记录
					if(hour2<=12 ){
						Record12OneDay record12=new Record12OneDay();
						record12.setDate1(date);
						record.add(record12);
						//看看中间相差多少天，若只差一天，则说明没有漏打卡
						Calendar c=Calendar.getInstance();
						c.setTime(date2);
						if(day2-day1>1){
							//下班漏打卡
							clockOutLost.add(c.getTime());
						}
						for(int i=2;i<day2-day1;i++){
							//无打卡记录
							c.add(Calendar.DAY_OF_MONTH,1);
							absent.add(c.getTime());							
						}
						if(day2-day1>1){
							c.add(Calendar.DAY_OF_MONTH,1);
							clockInLost.add(c.getTime());
						}
					}else{
						Record12OneDay record12=new Record12OneDay();
						record12.setDate2(date);
						record.add(record12);
						//看看中间相差多少天，若只差一天，则说明没有漏打卡
						Calendar c=Calendar.getInstance();
						c.setTime(date2);
						//下班漏打卡，无打卡记录
						clockOutLost.add(c.getTime());
						for(int i=1;i<day2-day1;i++){
							c.add(Calendar.DAY_OF_MONTH,1);
							absent.add(c.getTime());							
						}
					}	
					
				}else{
					//是同一天时，则当
					//1.hour1位于12点前时：
						//（1）当hour2位于12点前时，记录最后一次12点前的记录，
						//（2）当hour2位于12点后时，记录第一次12点后的记录
					//2.hour1位于12点后时判断有没有下班卡
					//因为是上班卡，所以在同一天的上班卡只记录第一条记录
					/*if(hour1<=12){
						if(hour2<=12){
							record12OneDay.setDate1(date);								
						}else{
							record12OneDay.setDate2(date);
						}
					}else{
						if(date1==null){
							//说明漏了下班卡
							calendar1.add(Calendar.DAY_OF_MONTH, -1);
							clockOutLost.add(calendar1.getTime());
						}
					}*/
				}
			}else{
				//date2==null
				//上一次打卡记录为空时，说明可能是下午上班的卡或是没有打下午上班的卡
				calendar1.setTime(date1);
				int day1=calendar1.get(Calendar.DAY_OF_MONTH);
				if(day1==1 && calendar1.get(Calendar.MONTH)==month){
					Calendar c=Calendar.getInstance();
					c.setTime(date);
					c.add(Calendar.MONTH, -1);
					day1=c.getActualMaximum(Calendar.DAY_OF_MONTH)+day1;
				}
				int hour1=calendar1.get(Calendar.HOUR_OF_DAY);
				boolean theSameDay=day1==day2;
				if(!theSameDay){
					//如果不是同一天，则当hour2位于12点前时，记录最后一次12点前的记录，当hour2位于12点后时，记录第一次12点后的记录
					if(hour2<=12 ){
						Record12OneDay record12=new Record12OneDay();
						record12.setDate1(date);
						record.add(record12);
						//说明漏打了上班的卡
						if(day2-day1==1){
							clockInLost.add(date1);
						}else{
							for(int i=1;i<day2-day1;i++){
								absent.add(calendar1.getTime());
								calendar1.add(Calendar.DAY_OF_MONTH, 1);
							}
							clockInLost.add(calendar1.getTime());
						}	
					}else{
						Record12OneDay record12=new Record12OneDay();
						record12.setDate2(date);
						record.add(record12);
						for(int i=1;i<=day2-day1;i++){
							absent.add(calendar1.getTime());
							calendar1.add(Calendar.DAY_OF_MONTH, 1);
						}												
					}
				}else{
					//是同一天时，则当
					//1.hour1位于12点前时：
						//（1）当hour2位于12点前时，记录最后一次12点前的记录，
						//（2）当hour2位于12点后时，记录第一次12点后的记录
					//2.hour1位于12点后时不需要处理
					if(hour1<=12){
						if(hour2<=12){
							record12OneDay.setDate1(date);								
						}else{
							record12OneDay.setDate2(date);							
						}
					}
				}			 
			}
			
			
		}else{
			Record12OneDay record12OneDay=new Record12OneDay();
			Calendar calendar=Calendar.getInstance();
			calendar.setTime(date);
			if(calendar.get(Calendar.HOUR_OF_DAY)>12){
				record12OneDay.setDate2(date);
			}else{
				Calendar c=Calendar.getInstance();
				c.setTime(date);
				c.add(Calendar.DAY_OF_MONTH, -1);
				record12OneDay.setDate1(date);
			}
			record.add(record12OneDay);
			int day=calendar.get(Calendar.DAY_OF_MONTH);
			if(day!=1){
				for(int i=1;i<day;i++){
					calendar.set(Calendar.DAY_OF_MONTH, i);
					absent.add(calendar.getTime());
				}
			}
		}
	}
	public List<Date> getAbsent() {
		return absent;
	}
	public List<Date> getClockInLost() {
		return clockInLost;
	}
	public List<Date> getClockOutLost() {
		return clockOutLost;
	}
	public List<Record12OneDay> getRecord() {
		return record;
	}
	public void setAbsent(List<Date> absent) {
		this.absent = absent;
	}
	public void setClockInLost(List<Date> clockInLost) {
		this.clockInLost = clockInLost;
	}
	public void setClockOutLost(List<Date> clockOutLost) {
		this.clockOutLost = clockOutLost;
	}
	public void setRecord(List<Record12OneDay> record) {
		this.record = record;
	}
	
	public String toString(){
		StringBuilder sb=new StringBuilder("\n"+dept+"->"+name+"(姓名)->"+recordnum+"(登记号码)的打卡记录为：");
		if(clockInLost.size()>0){
			sb.append("\n上班漏打卡：");
			for(Date d:clockInLost){
				sb.append(dfYYYYMMDD.format(d)).append(",");
			}			
		}
		if(clockOutLost.size()>0){
			sb.append("\n下班漏打卡：");
			for(Date d:clockOutLost){
				sb.append(dfYYYYMMDD.format(d)).append(",");
			}			
		}
		if(absent.size()>0){
			sb.append("\n无打卡记录：");
			for(Date d:absent){
				sb.append(dfYYYYMMDD.format(d)).append(",");
			}			
		}
		if(sb.length()==0){
			sb.append("全勤！");
		}
		/*if(sb.length()>0){
			sb.insert(0, dept+"->"+name+"[登记号码"+recordnum+"]的打卡记录为：");
		}*/
		/*if(record.size()>0){
			sb.append("\n打卡记录：");
			for(Record12OneDay d:record){
				if(d.getDate1()!=null){
					sb.append(Test.df.format(d.getDate1())).append(",");					
				}
				if(d.getDate2()!=null){
					sb.append(Test.df.format(d.getDate2())).append(",");
				}
			}			
		}*/
		return sb.toString();
	}
}
